#! /bin/bash
set -e

# get local IP.
localIp=`/sbin/ip route  | /bin/grep 'scope link \{1,\}src '| /bin/awk '{print substr($0,index($0,"src "))}'|/bin/grep 'src 10\.\|src 172\.\|src 192\.\|src 100\.\|src 9\.' | /bin/awk '{ print $2}'|sed -n '1p'`
if [[ -z $localIp ]]; then
    echo 'localIp is empty'
    exit 1
fi

# make log-dir if not exists.
if [[ ! -d "${logDir}" ]];then
    mkdir ${logDir}
fi

# make module executable.
chmod +x ${moduleName}

sed -i 's/${localIp}/'${localIp}'/g' ${configFile}

test -f ${logDir}/${moduleName}.log && cp -f ${logDir}/${moduleName}.log ${logDir}/${moduleName}.log.1
# bring module up.
./${moduleName} -f ${configFile} > ${logDir}/${moduleName}.log 2>&1 &
